<?php

namespace App\Store\Controllers\Account;

use App\Admin\Models\SiteStore;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Common\Models\StoreRenewalLog;
use App\Common\Services\Store\Common as StoreCommon;
use App\Admin\Models\AdminPaytypeList;

class StoreFeeController extends Controller
{
    /**
     * 获取续费列表
     */
    public function getlist(Request $request)
    {
        $list = StoreRenewalLog::getListByStore($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 获取续费详情
     */
    public function getinfo(Request $request)
    {
        $id = $request->id ?? 0;
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $info = StoreRenewalLog::where(['id' => $id, 'cp_id' => $cp_id])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['info' => $info]]);
    }

    /**
     * 续费操作
     */
    public function dopayfee(Request $request)
    {
        $id = $request->id ?? 0;
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $info = StoreRenewalLog::where(['id' => $id, 'cp_id' => $cp_id])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '续费记录不存在']);
        }
        if ($info->is_pay != 1) {
            return response()->json(['code' => '001', 'msg' => '续费订单已支付，请确认']);
        }
        $pay_type = $request->pay_type ?? 0;
        $pay_img = $request->pay_img ?? '';
        $payinfo = AdminPaytypeList::where(['id' => $pay_type, 'is_del' => 1])->first();
        if (!$payinfo) {
            return response()->json(['code' => '001', 'msg' => '无效付款方式']);
        }

        if ($payinfo->type!=3&&$pay_img == '') {
            return response()->json(['code' => '001', 'msg' => '付款截图未上传']);
        }

        StoreRenewalLog::where([['id', $id]])->update(['is_pay' => 2, 'pay_at' => time(), 'pay_img' => $pay_img, 'pay_type' => $pay_type, 'pay_type_name' => $payinfo->paytype_name]);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }
    /**
     * 获取续费支付方式列表
     */
    public function getpaytypelist(Request $request)
    {
        $list = AdminPaytypeList::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }
}
